package cool.webstudy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import cool.webstudy.admin.constant.enums.ConfigTypeEnum;
import cool.webstudy.admin.constant.enums.DelFlagEnum;
import cool.webstudy.admin.constant.enums.rescode.ConfigResCodeEnum;
import cool.webstudy.admin.exception.BusinessException;
import cool.webstudy.admin.mapper.config.ConfigMapper;
import cool.webstudy.admin.model.dto.config.CreateConfigDTO;
import cool.webstudy.admin.model.dto.config.UpdateConfigDTO;
import cool.webstudy.admin.model.dto.user.UserDetailInfoDTO;
import cool.webstudy.admin.model.po.config.ConfigPO;
import cool.webstudy.admin.service.ConfigService;
import cool.webstudy.admin.utils.UserDetailUtil;
import cool.webstudy.common.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * @author 莫振双
 * @date 2025/1/4 15:47
 * @description: 配置模块服务实现类
 */
@Service
public class ConfigServiceImpl implements ConfigService {
    @Autowired
    private ConfigMapper configMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createConfig(CreateConfigDTO dto) {
        if (Objects.isNull(dto)) {
            return false;
        }
        UserDetailInfoDTO userDetailInfo = UserDetailUtil.getUserDetailInfo();
        ConfigPO configPO = new ConfigPO();
        configPO.setUnCode(UUIDUtil.getUUID());
        configPO.setType(dto.getType());
        configPO.setContent(dto.getContent());
        configPO.setCreateBy(userDetailInfo.getAccount());
        configPO.setUpdateBy(userDetailInfo.getAccount());
        configPO.setCreateTime(new Date());
        configPO.setUpdateTime(new Date());
        configPO.setDelFlag(DelFlagEnum.NOT_DEL_FLAG.getFlag());
        try {
            configMapper.insert(configPO);
        } catch (Exception e) {
            throw new BusinessException(ConfigResCodeEnum.RC_ERROR_CREATE);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteConfig(ConfigTypeEnum type) {
        if (Objects.isNull(type)) {
            return false;
        }
        UserDetailInfoDTO userDetailInfo = UserDetailUtil.getUserDetailInfo();
        LambdaUpdateWrapper<ConfigPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ConfigPO::getType, type)
                .set(ConfigPO::getDelFlag, DelFlagEnum.DEL_FLAG.getFlag())
                .set(ConfigPO::getUpdateBy, userDetailInfo.getAccount())
                .set(ConfigPO::getUpdateTime, new Date());
        try {
            configMapper.update(null, updateWrapper);
        } catch (Exception e) {
            throw new BusinessException(ConfigResCodeEnum.RC_ERROR_DELETE);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateConfig(UpdateConfigDTO dto) {
        if (Objects.isNull(dto)) {
            return false;
        }
        UserDetailInfoDTO userDetailInfo = UserDetailUtil.getUserDetailInfo();
        LambdaUpdateWrapper<ConfigPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ConfigPO::getType, dto.getType())
                .set(ConfigPO::getContent, dto.getContent())
                .set(ConfigPO::getUpdateBy, userDetailInfo.getAccount())
                .set(ConfigPO::getUpdateTime, new Date());
        try {
            configMapper.update(null, updateWrapper);
        } catch (Exception e) {
            throw new BusinessException(ConfigResCodeEnum.RC_ERROR_UPDATE);
        }
        return true;
    }

    @Override
    public String queryConfig(ConfigTypeEnum type) {
        if (Objects.isNull(type)) {
            throw new BusinessException(ConfigResCodeEnum.RC_ERROR_QUERY_PARAM_MISS);
        }
        LambdaQueryWrapper<ConfigPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfigPO::getType, type)
                .eq(ConfigPO::getDelFlag, DelFlagEnum.NOT_DEL_FLAG.getFlag());
        try {
            ConfigPO configPO = configMapper.selectOne(queryWrapper);
            if (Objects.isNull(configPO)) {
                throw new BusinessException(ConfigResCodeEnum.RC_CONFIG_NOT_EXIST, type.getDesc());
            }
            return configPO.getContent();
        } catch (Exception e) {
            throw new BusinessException(ConfigResCodeEnum.RC_ERROR_QUERY);
        }
    }
}
